local damage = require 'types.damage'
local random = math.pseudo_random

local mt = damage.__index 

mt.damage_type = '物理'

--护甲因子
mt.def_sub = 0.01
mt.attack_rate = 1


function get_ex_attack(source,types,data)
    local ad1,ap1 = source:get('物理附伤'), source:get('魔法附伤')
    local ad2,ap2 = source:get('物理伤害'), source:get('魔法伤害')
    local ad3,ap3 = 0, 0
    local atk = source:get('攻击')
    if types=='物理' then
        return ad1 + atk,ad2,ad3
    elseif types=='魔法' then
        return ap1 + atk,ap2,ap3
    elseif types=='混合' then
        return (ad1 + ap1 + atk),(ad2+ap2),(ad3+ap3)
    elseif types=='通用' then
        return (ad1 + ap1)/2 + atk,(ad2 + ap2)/2,(ad3 + ap3)/2
    elseif types=='适应' then
        if ad2>ap2 then
            if data then
                data.damage_type = '物理'
            end
            return ad1 + atk,ad2,ad3
        else
            if data then
                data.damage_type = '魔法'
            end
            return ap1 + atk,ap2,ap3
        end
    end
    return atk,0,0
end

local get_ex_damage = function(source,types,data)
    local ad2,ap2 = source:get('普攻输出'), source:get('技能输出')
    local ad3,ap3 = 0, 0
    if types=='普攻' then
        return ad2,ad3
    elseif types=='技能' then
        return ad2,ap3
    elseif types=='混合' then
        return (ad2+ap2),(ad3+ap3)
    elseif types=='通用' then
        return (ad2 + ap2)/2,(ad3 + ap3)/2
    elseif types=='适应' then
        if ad2>ap2 then
            if data then
                data.attack_type = '物理'
            end
            return ad2,ad3
        else
            if data then
                data.attack_type = '魔法'
            end
            return ad3,ap3
        end
    end
    return 0,0
end

--[本地图技能伤害默认为攻击倍率]
--[特殊处理下数值]
function mt:get_base_damage()
    local source = self.source
    local damage = self.damage
    if self:is_attack() or self.skill==nil then
        local spd1 = math.max(source:get('攻击速度'),100)
        local spd2 = math.max(source:get('冷却速度'),100)
        self.attack_type = source:get_data('默认输出') or '普攻'
        self.damage_type = source:get_data('默认伤害') or '物理'
        if spd2>spd1 then
            self:mul((spd2 - spd1)/spd1*100)
        end
    end
    if self.skill then
        self.attack_rate = damage/100
        damage = source:get('攻击') * self.attack_rate
    end
    
    --伤害类型
    local ad1,ad2,ad3 = get_ex_attack(source,self.damage_type,self)
    damage = ad1
    self.damage_add = ad2
    self.damage_deep = ad3
    

    --输出类型
    local ad1,ad2 = get_ex_damage(source,self.attack_type,self)
    self.attack_add = ad1
    self.attack_deep = ad2
    
    return damage
end


function damage:damageCommon()
    local source = self.source
    local target = self.target
    local skill = self.skill
    local armor = target:get('护甲')


    self:mul(self.damage_add)
    self:mul(self.damage_deep)
    self:mul(self.attack_add)
    self:mul(self.attack_deep)

    self:div(source:get('伤害增幅'))
    
    --护甲减免
    self:div( 100*armor/(100 + armor))


end


function ac.unit.__index:get_test_dps()
    local attack_type = self:get_data('默认输出') or '普攻'
    local damage_type = self:get_data('默认伤害') or '物理'
    
    --伤害类型
    local ex,dmg1,dmg2 = get_ex_attack(self,damage_type)
    
    --输出类型
    local atk1,atk2 = get_ex_damage(self,attack_type)

    local damage = (ex) * (1 + dmg1*0.01) * (1 + dmg2*0.01) * (1 + atk1*0.01) * (1 + atk2*0.01)
    local dps1 = damage * self:get('攻击速度')*0.01
    local dps2 = damage * self:get('冷却速度')*0.01
    return dps1,dps2
end

